//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "RangeAccrualPricerByBgm.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Termstructures/Volatility/SmileSection.h>
#include <gen/QL/Cashflows/FloatingRateCoupon.h>
#include <gen/QL/Cashflows/RangeAccrualPricer.h>
using namespace Cephei::QL::Termstructures::Volatility;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CRangeAccrualPricerByBgm::CRangeAccrualPricerByBgm (Double correlation, Cephei::QL::Termstructures::Volatility::ISmileSection^ smilesOnExpiry, Cephei::QL::Termstructures::Volatility::ISmileSection^ smilesOnPayment, Boolean withSmile, Boolean byCallSpread) : CRangeAccrualPricer(CRangeAccrualPricerByBgm::typeid)
{
    CSmileSection^ _CsmilesOnExpiry;
    CSmileSection^ _CsmilesOnPayment;
    try
    {
#ifdef HANDLE
        _phRangeAccrualPricerByBgm = NULL;
#endif
        QuantLib::Real _correlation = (QuantLib::Real)ValueHelper::Convert (correlation); //d
        _CsmilesOnExpiry = safe_cast<CSmileSection^> (smilesOnExpiry);
        _CsmilesOnExpiry->Lock();
        boost::shared_ptr<QuantLib::SmileSection>& _smilesOnExpiry = static_cast<boost::shared_ptr<QuantLib::SmileSection>&> (_CsmilesOnExpiry->GetShared ()); 
        _CsmilesOnPayment = safe_cast<CSmileSection^> (smilesOnPayment);
        _CsmilesOnPayment->Lock();
        boost::shared_ptr<QuantLib::SmileSection>& _smilesOnPayment = static_cast<boost::shared_ptr<QuantLib::SmileSection>&> (_CsmilesOnPayment->GetShared ()); 
        bool _withSmile = (bool)ValueHelper::Convert (withSmile); //d
        bool _byCallSpread = (bool)ValueHelper::Convert (byCallSpread); //d
        _ppRangeAccrualPricerByBgm = new boost::shared_ptr<QuantLib::RangeAccrualPricerByBgm> (new QuantLib::RangeAccrualPricerByBgm ( _correlation,  _smilesOnExpiry,  _smilesOnPayment,  _withSmile,  _byCallSpread ));
        SetRangeAccrualPricer (boost::dynamic_pointer_cast<QuantLib::RangeAccrualPricer> (*_ppRangeAccrualPricerByBgm));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CsmilesOnExpiry != nullptr) _CsmilesOnExpiry->Unlock();
        if (_CsmilesOnPayment != nullptr) _CsmilesOnPayment->Unlock();
    }
}
Cephei::QL::Cashflows::CRangeAccrualPricerByBgm::CRangeAccrualPricerByBgm (boost::shared_ptr<QuantLib::RangeAccrualPricerByBgm>& childNative, Object^ owner) : CRangeAccrualPricer(CRangeAccrualPricerByBgm::typeid)
{
#ifdef HANDLE
	_phRangeAccrualPricerByBgm = NULL;
#endif
	_ppRangeAccrualPricerByBgm = &childNative;
    _ppRangeAccrualPricer = new boost::shared_ptr<QuantLib::RangeAccrualPricer> (boost::dynamic_pointer_cast<QuantLib::RangeAccrualPricer> (*_ppRangeAccrualPricerByBgm));
}
Cephei::QL::Cashflows::CRangeAccrualPricerByBgm::CRangeAccrualPricerByBgm (QuantLib::RangeAccrualPricerByBgm& childNative, Object^ owner) : CRangeAccrualPricer(CRangeAccrualPricerByBgm::typeid)
{
#ifdef HANDLE
	_phRangeAccrualPricerByBgm = NULL;
#endif
	_ppRangeAccrualPricerByBgm = new boost::shared_ptr<QuantLib::RangeAccrualPricerByBgm> (&childNative);
    _ppRangeAccrualPricer = new boost::shared_ptr<QuantLib::RangeAccrualPricer> (boost::dynamic_pointer_cast<QuantLib::RangeAccrualPricer> (*_ppRangeAccrualPricerByBgm));
    _RangeAccrualPricerByBgmOwner = owner;
    _RangeAccrualPricerOwner = owner;
}

Cephei::QL::Cashflows::CRangeAccrualPricerByBgm::CRangeAccrualPricerByBgm (CRangeAccrualPricerByBgm^ copy) : CRangeAccrualPricer(CRangeAccrualPricerByBgm::typeid)
{
#ifdef HANDLE
	_phRangeAccrualPricerByBgm = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppRangeAccrualPricerByBgm = new boost::shared_ptr<QuantLib::RangeAccrualPricerByBgm> (copy->GetShared());
        _ppRangeAccrualPricer = new boost::shared_ptr<QuantLib::RangeAccrualPricer> (boost::dynamic_pointer_cast<QuantLib::RangeAccrualPricer> (*_ppRangeAccrualPricerByBgm));
    }
}
Cephei::QL::Cashflows::CRangeAccrualPricerByBgm::CRangeAccrualPricerByBgm (PLATFORM::Type^ t) : CRangeAccrualPricer(CRangeAccrualPricerByBgm::typeid)
{
#ifdef HANDLE
	_phRangeAccrualPricerByBgm = NULL;
#endif
	if (!t->IsSubclassOf(CRangeAccrualPricerByBgm::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CRangeAccrualPricerByBgm::CRangeAccrualPricerByBgm (QuantLib::Handle<QuantLib::RangeAccrualPricerByBgm>& childNative, Object^ owner)  : CRangeAccrualPricer(CRangeAccrualPricerByBgm::typeid)
{
	_phRangeAccrualPricerByBgm = &childNative;
	_ppRangeAccrualPricerByBgm = &static_cast<boost::shared_ptr<QuantLib::RangeAccrualPricerByBgm>>(childNative.currentLink());
    _ppRangeAccrualPricer = new boost::shared_ptr<QuantLib::RangeAccrualPricer> (boost::dynamic_pointer_cast<QuantLib::RangeAccrualPricer> (*_ppRangeAccrualPricerByBgm));
    _RangeAccrualPricerByBgmOwner = owner;
}
Cephei::QL::Cashflows::CRangeAccrualPricerByBgm::CRangeAccrualPricerByBgm (QuantLib::Handle<QuantLib::RangeAccrualPricerByBgm> childNative)  : CRangeAccrualPricer(CRangeAccrualPricerByBgm::typeid)
{
	_phRangeAccrualPricerByBgm = &childNative;
	_ppRangeAccrualPricerByBgm = &static_cast<boost::shared_ptr<QuantLib::RangeAccrualPricerByBgm>>(childNative.currentLink());
    _ppRangeAccrualPricer = new boost::shared_ptr<QuantLib::RangeAccrualPricer> (boost::dynamic_pointer_cast<QuantLib::RangeAccrualPricer> (*_ppRangeAccrualPricerByBgm));
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CRangeAccrualPricerByBgm::CRangeAccrualPricerByBgm (QuantLib::RangeAccrualPricerByBgm childNative)  : CRangeAccrualPricer(CRangeAccrualPricerByBgm::typeid)
{
#ifdef HANDLE
	_phRangeAccrualPricerByBgm = NULL;
#endif
	_ppRangeAccrualPricerByBgm = new boost::shared_ptr<QuantLib::RangeAccrualPricerByBgm> (new QuantLib::RangeAccrualPricerByBgm (childNative));
    _ppRangeAccrualPricer = new boost::shared_ptr<QuantLib::RangeAccrualPricer> (boost::dynamic_pointer_cast<QuantLib::RangeAccrualPricer> (*_ppRangeAccrualPricerByBgm));
}
#endif

Cephei::QL::Cashflows::CRangeAccrualPricerByBgm::~CRangeAccrualPricerByBgm ()
{
    if (_ppRangeAccrualPricerByBgm != NULL)
    {
	    delete _ppRangeAccrualPricerByBgm;
        _ppRangeAccrualPricerByBgm = NULL;
    }
}
Cephei::QL::Cashflows::CRangeAccrualPricerByBgm::!CRangeAccrualPricerByBgm ()
{
    if (_ppRangeAccrualPricerByBgm != NULL)
    {
	    delete _ppRangeAccrualPricerByBgm;
    }
}
QuantLib::RangeAccrualPricerByBgm& Cephei::QL::Cashflows::CRangeAccrualPricerByBgm::GetReference ()
{
    if (_ppRangeAccrualPricerByBgm == NULL) throw REFNEW NativeNullException ();
	return **_ppRangeAccrualPricerByBgm;
}
boost::shared_ptr<QuantLib::RangeAccrualPricerByBgm>& Cephei::QL::Cashflows::CRangeAccrualPricerByBgm::GetShared ()
{
    if (_ppRangeAccrualPricerByBgm == NULL) throw REFNEW NativeNullException ();
	return *_ppRangeAccrualPricerByBgm;
}
QuantLib::RangeAccrualPricerByBgm* Cephei::QL::Cashflows::CRangeAccrualPricerByBgm::GetPointer ()
{
    if (_ppRangeAccrualPricerByBgm == NULL) throw REFNEW NativeNullException ();
	return &**_ppRangeAccrualPricerByBgm;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::RangeAccrualPricerByBgm>& Cephei::QL::Cashflows::CRangeAccrualPricerByBgm::GetHandle ()
{
	if (_phRangeAccrualPricerByBgm == NULL)
	{
		_phRangeAccrualPricerByBgm = new Handle<QuantLib::RangeAccrualPricerByBgm> (*_ppRangeAccrualPricerByBgm);
	}
	return *_phRangeAccrualPricerByBgm;
}
#endif
bool Cephei::QL::Cashflows::CRangeAccrualPricerByBgm::HasNative () 
{
	return (_ppRangeAccrualPricerByBgm != NULL);
}

Double Cephei::QL::Cashflows::CRangeAccrualPricerByBgm::SwapletPrice::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppRangeAccrualPricerByBgm)->swapletPrice ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Cashflows::IRangeAccrualPricerByBgm^ Cephei::QL::Cashflows::CRangeAccrualPricerByBgm_Factory::Create (Double correlation, Cephei::QL::Termstructures::Volatility::ISmileSection^ smilesOnExpiry, Cephei::QL::Termstructures::Volatility::ISmileSection^ smilesOnPayment, Boolean withSmile, Boolean byCallSpread)
{
    return REFNEW CRangeAccrualPricerByBgm ( correlation,  smilesOnExpiry,  smilesOnPayment,  withSmile,  byCallSpread);
}
